/**
 * Created by oneminute on 7/27/14.
 */

var assert = require("assert");
var should = require("should");
var logger = require("../../lib/utils/logger");
var testutils = require("../testutils");
var lz = require("../../lib/utils/modules")();
var httpMocks = require("../lib/http-mock");
var async = require("async");
var register = require("../../routes/register");

describe("测试routes/Register模块。", function () {
    var users = [];

    before(function(done) {
        done();
    });

    it("测试正常注册的情况。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/search',
            body: {
                email: "register_req_0001@lz.com",
                password: "123456"
            }
        });
        var response = httpMocks.createExpressResponse();

        register.post(request, response, {}, function(_err, _user, _args) {
            testutils.check(done, function () {
                users.push(_user);
                assert.notEqual(_user, null);
                _user.password.should.equal("123456");
                _user.email.email.should.equal("register_req_0001@lz.com");
            });
        });
    });

    it("测试Email已存在的情况。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/search',
            body: {
                email: "register_req_0001@lz.com",
                password: "123456"
            }
        });
        var response = httpMocks.createExpressResponse();

        register.post(request, response, {}, function(_err, _user, _args) {
            testutils.check(done, function () {
                _err.code.should.equal(lz.Config.errors.REGISTER_EMAIL_EXISTED.code);
            });
        });
    });

    it("测试没有传入Email的情况。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/search',
            body: {
                password: "123456"
            }
        });
        var response = httpMocks.createExpressResponse();

        register.post(request, response, {}, function(_err, _user, _args) {
            testutils.check(done, function () {
                _err.code.should.equal(lz.Config.errors.REGISTER_NO_EMAIL.code);
            });
        });
    });

    after(function (done) {
        var tasks = [];
        users.forEach(function(_user) {
            tasks.push(function(_cb) {
                _user.delete(function() {
                    _cb(null);
                });
            });
        });
        async.series(tasks, function() {
            done();
        });
    });
});